<template>
    <div class="main-container">
        <div class="main-content">
            <div class="row">
                <div class="col-sm-10 col-sm-offset-1">
                    <div class="login-container">
                        <div class="center">
                            <div class="space-30"></div>
                            <h1>
                                <!--								<i class="ace-icon fa fa-leaf green"></i>-->
                                <span class="grey">Coding后台管理系统</span>
                            </h1>
                        </div>

                        <div class="space-6"></div>

                        <div class="position-relative">
                            <div id="login-box" class="login-box visible widget-box no-border">
                                <div class="widget-body">
                                    <div class="widget-main">
                                        <h4 class="header blue lighter bigger center">
                                            <i class="fa fa-pencil "></i>
                                            <span class="grey" style="font-family: 'Courier New',serif">请输入用户名密码</span>
                                        </h4>

                                        <div class="space-6"></div>

                                        <form>
                                            <fieldset>
                                                <label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input v-model="user.loginName" type="text"
                                                                   class="form-control"
                                                                   placeholder="用户名"/>
															<i class="ace-icon fa fa-user"></i>
														</span>
                                                </label>

                                                <label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input v-model="user.password" type="password"
                                                                   class="form-control"
                                                                   placeholder="密码"/>
															<i class="ace-icon fa fa-lock"></i>
														</span>
                                                </label>
                                                <label class="block clearfix">
                                                      <span class="block input-icon input-icon-right">
                                                        <div class="input-group">
                                                          <input  v-model="user.imageCode" type="text"
                                                                 class="form-control" placeholder="验证码" >
                                                          <span class="input-group-addon" id="basic-addon2">
                                                            <img v-on:click="loadImageCode()" id="image-code"
                                                                 alt="验证码"/>
                                                          </span>
                                                        </div>
                                                      </span>
                                                </label>

                                                <div class="space"></div>

                                                <div class="clearfix">
                                                    <label class="inline">
                                                        <input v-model="remember" type="checkbox" class="ace"/>
                                                        <span class="lbl">记住我</span>
                                                    </label>

                                                    <button type="button"
                                                            class="width-35 pull-right btn btn-sm btn-primary"
                                                            @click="login()">
                                                        <!--                                                        <i class="ace-icon fa fa-pencil"></i>-->
                                                        <span class="bigger-110">登录</span>
                                                    </button>
                                                </div>

                                                <div class="space-4"></div>
                                            </fieldset>
                                        </form>
                                    </div><!-- /.widget-main -->

                                </div><!-- /.widget-body -->
                            </div><!-- /.login-box -->

                        </div><!-- /.position-relative -->
                    </div>
                </div><!-- /.col -->
            </div><!-- /.row -->
        </div><!-- /.main-content -->
    </div>
</template>

<script>

    export default {
        name: "login",
        data: function () {
            return {
                user: {},
                remember: true,
                imageCodeToken: ""
            }
        },
        mounted: function () {
            let _this = this;
            $('body').removeClass('no-skin');
            $('body').attr('class', 'login-layout light-login');
            let rememberUser = LocalStorage.get(LOCAL_KEY_REMEMBER_USER);
            if (rememberUser) {
                _this.user = rememberUser;
            }
            // 初始时加载一次验证码图片
            _this.loadImageCode();
        },
        methods: {
            login() {
                let _this = this;
                if (!Validator.require(_this.user.loginName, "用户名")
                    || !Validator.length(_this.user.loginName, "用户名", 1, 50)) {
                    return;
                }
                if (!Validator.require(_this.user.password, "密码")
                    || !Validator.length(_this.user.password, "密码", 1, 32)) {
                    return;
                }
                if (!Validator.require(_this.user.imageCode, "验证码")) {
                    return;
                }
                if (_this.user.imageCode.length !== 4) {
                    Toast.warning("验证码长度必须为4");
                    return;
                }
                //对当前密码进行盐值计算，然后跟LocalStorage中的盐值比较
                //不同说明用户修改过了
                //相同说明用户没修改过
                //如果密码跟缓存【缓存的已经加过密】的一样
                let md5 = hex_md5(_this.user.password);
                let rememberUser = LocalStorage.get(LOCAL_KEY_REMEMBER_USER) || {};
                if (md5 !== rememberUser.md5) {
                    //md5不一致说明密码输入修改了，需要重新计算md5
                    _this.user.password = hex_md5(_this.user.password + ran, KEY);
                }
                _this.user.imageCodeToken = _this.imageCodeToken;
                _this.$ajax.post(process.env.VUE_APP_SERVER + '/system/admin/user/login/',
                    _this.user).then((response) => {
                    let resp = response.data;
                    if (resp.success) {
                        Toast.success(response.data.message);
                        let loginUser = resp.data;
                        Tool.setLoginUser(loginUser);
                        let r_md5 = hex_md5(_this.user.password);
                        if (_this.remember) {
                            //保存当前的用户名、密码、密码的盐值码【用于检测密码是否修改过】
                            //password:_this.user.password 这里的密码是加密过的
                            //md5:对当前的密码计算盐值码，保存到缓存中
                            //下次登录的时候会在输入框显示缓存中的值
                            //对此时的输入框计算盐值码，跟缓存的比较，来判断用户是否重新输入过
                            LocalStorage.set(LOCAL_KEY_REMEMBER_USER, {
                                loginName: loginUser.loginName,
                                password: _this.user.password,
                                md5: r_md5
                            });
                        } else {
                            LocalStorage.set(LOCAL_KEY_REMEMBER_USER, null);
                        }
                        this.$router.push("/welcome");
                    } else {
                        Toast.error(response.data.message);
                        _this.user.password = "";
                        _this.user.imageCode = "";
                        _this.loadImageCode();
                    }
                });
            },

            /*加载图形验证码*/
            loadImageCode: function () {
                let _this = this;
                _this.imageCodeToken = Tool.uuid(8);
                $('#image-code').attr('src', process.env.VUE_APP_SERVER + '/system/admin/kaptcha/image-code/' + _this.imageCodeToken);
            },
        }

    }
</script>

<style scoped>
    .input-group-addon {
        padding: 0;
    }
</style>
